package com.goodwill.androidtest;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.Calendar;
import java.util.Date;

/**
 * Author: Created by guoxiaojun on 2019/10/9-14:45
 * Description: 基于开源库Android-PickerView封装的时间选择控件
 *
 * @see {https://github.com/Bigkoo/Android-PickerView}
 */
public class CustomTimePicker {
    private static boolean mIsInitResources = true; //标记需要初始化日期控件的资源
    private OnTimeSelectCallback callback;
    private static Calendar mStartDate;
    private static Calendar mEndDate;
    private static Calendar mSelectedDate;
    /**
     * 标题背景颜色
     */
    private static int mTitleBgColor;
    /**
     * 标题文字颜色
     */
    private static int mTitleTextColor;
    /**
     * 滚轮背景颜色
     */
    private static int mRollBgColor;
    /**
     * 滚轮选中部分文字的颜色
     */
    private static int mRollSelectTextColor;
    /**
     * 滚轮未选中部分文字的颜色
     */
    private static int mRollNormalTextColor;

    /**
     * 日期选择的回调
     */
    public interface OnTimeSelectCallback {
        void onTimeSelect(Date date, View view);
    }

    /**
     * 初始化时间控件
     *
     * @param context 上下文对象
     * @param callback 时间选择的回调
     * @return
     */
    public static TimePickerView initTimePicker(Context context, OnTimeSelectCallback callback) {
        initResources();
        initCalendar();
        return initTimePicker(context, mSelectedDate, mStartDate, mEndDate, callback, TimePickerType.TYPE_ALL);
    }

    /**
     * 初始化日期控件的属性
     */
    private static void initResources() {
        if (mIsInitResources) {
            mIsInitResources = false;
            Resources resources = MyApplication.getContent().getResources();
            if (resources != null) {
                mTitleBgColor = resources.getColor(R.color.color_3CA0FF);
                mTitleTextColor = resources.getColor(R.color.color_FFFFFF);
                mRollBgColor = resources.getColor(R.color.color_FFFFFF);
                mRollSelectTextColor = resources.getColor(R.color.color_37B3FD);
                mRollNormalTextColor = resources.getColor(R.color.color_666666);
            }
        }
    }

    /**
     * 初始化默认的Calendar
     */
    private static void initCalendar() {
        mSelectedDate = Calendar.getInstance();
        mStartDate = Calendar.getInstance();
        mEndDate = Calendar.getInstance();

        mStartDate.set(Calendar.MONTH, -120);
        //当前日期上浮三天,以便可以选择未来三天日期
        mEndDate.add(Calendar.DATE, 3);
    }

    /**
     * 初始化时间控件
     *
     * @param context 上下文对象
     * @param callback 时间选择的回调
     * @param type 日期选择的类型
     * @return
     */
    public static TimePickerView initTimePicker(Context context, OnTimeSelectCallback callback, @TimePickerType int type) {
        initCalendar();
        return initTimePicker(context, mSelectedDate, mStartDate, mEndDate, callback, type);
    }

    /**
     * 初始化时间控件
     *
     * @param context 上下文对象
     * @param selectedDate
     * @param startDate
     * @param endDate
     * @param callback 时间选择的回调
     * @param type 日期选择的类型
     * @return
     */
    public static TimePickerView initTimePicker(Context context, Calendar selectedDate, Calendar startDate, Calendar endDate, OnTimeSelectCallback callback, @TimePickerType int type) {
        TimePickerView timePicker = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (callback != null) {
                    callback.onTimeSelect(date, v);
                }
            }
        }).setType(getArrayByType(type))// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setTitleText("日期选择")//标题文字
                .isCyclic(false)
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .setTitleColor(mTitleTextColor)//标题文字颜色
                .setSubmitColor(mTitleTextColor)//确定按钮文字颜色
                .setCancelColor(mTitleTextColor)//取消按钮文字颜色
                .setTitleBgColor(mTitleBgColor)//标题背景颜色 Night mode
                .setBgColor(mRollBgColor)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setTextColorOut(mRollNormalTextColor)
                .setTextColorCenter(mRollSelectTextColor)
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setGravity(Gravity.BOTTOM)
                .build();

        return timePicker;
    }

    /**
     * 根据转入的类型获取显示的数组
     *
     * @param type
     * @return
     */
    private static boolean[] getArrayByType(int type) {
        boolean[] arrayTypes = null;
        switch (type) {
            case TimePickerType.TYPE_ALL:
                //yyyy-MM-dd HH:mm
                arrayTypes = new boolean[]{true, true, true, true, true, false};
                break;
            case TimePickerType.TYPE_DATE:
                //格式为: yyyy-MM-dd
                arrayTypes = new boolean[]{true, true, true, false, false, false};
                break;
            case TimePickerType.TYPE_TIME:
                //格式为: HH:mm:ss
                arrayTypes = new boolean[]{false, false, false, true, true, true};
                break;
            default:
                arrayTypes = new boolean[]{true, true, true, true, true, false};
                break;
        }
        return arrayTypes;
    }
}
